/**
 * Created by hiephn on 2014/08/26.
 */
define( "jquery", function() {
    return jQuery;
});
requirejs.config({
    paths: {
        'text': '../plugins/requirejs-text/text',
        'durandal':'../plugins/durandal/js',
        'plugins' : '../plugins/durandal/js/plugins',
        'transitions' : '../plugins/durandal/js/transitions',
        'knockout': '../plugins/knockout.js/knockout',
        'knockout-mapping': '../plugins/knockout-mapping/knockout.mapping',
        'ko-ext': 'knockout-ext',
        'theme': 'theme',
        'modals': 'modals',
        'http': 'http',
        'conf': 'conf',
        'facebook': '//connect.facebook.net/en_US/all'
    },
    shim: {
        'http': 'modals',
        'facebook' : {
            exports: 'FB'
        }
    }
});

define(['durandal/system','durandal/app','http','conf','knockout','knockout-mapping','ko-ext','facebook'],function (system,app,http,conf,ko,koMap) {

    system.debug(true);

    app.title = 'Durandal Starter Kit';

    app.configurePlugins({
        router:true,
        dialog: true
    });

    app.ko = ko;
    app.koo = ko.observable;
    app.koa = ko.observableArray;
    app.koc = ko.computed;
    app.koMap = koMap;
    app.http = http;
    app.conf = conf;

    app.cache = {};
    app.http.get(
        app.conf.MNP_BACK_URL + '/provider', {}, {
            success: function(json) {
                app.cache.providers = json.dataList;
                app.cache.providerNames = {};
                var dataList = json.dataList;
                for (var i in dataList) {
                    app.cache.providerNames[dataList[i]['id']] = dataList[i].name;
                }
            }
        }
    );
    app.http.get(
        app.conf.MNP_BACK_URL + '/channel', {}, {
            success: function(json) {
                app.cache.channels = json.dataList;
                app.cache.channelFas = {};
                var dataList = json.dataList;
                for (var i in dataList) {
                    var channelFa = {};
                    app.cache.channelFas[dataList[i]['id']] = dataList[i].fa;
                }
            }
        }
    );

    app.user = function(options) {
        if (!this.data) {
            this.data = app.koMap.fromJS({
                name: null,
                birthday: null,
                email: null,
                imgUrl: null,
                id: null
            });
        }
        if (options && options.fb) {
            var fb = options.fb;
            this.data.name(fb.name);
            this.data.birthday(fb.birthday);
            this.data.email(fb.email);
            this.data.id(fb.id);
            if (fb.picture && fb.picture.data && fb.picture.data.url)
                this.data.imgUrl(fb.picture.data.url);
        }
        return this.data;
    };

    FB.init({
        appId      : '553981174731375',
        status     : true,
        xfbml      : true,
        cookie     : true,
        version    : 'v2.1'
    });

    app.start().then(function() {
        window.app = app;
        app.setRoot('shell');
    });
});